#Essentially applies the prerequisite patch at runtime by redefining the ARH
#form method.  Since it redefines the whole method new changes to that method
#in Rails won't affect this either way.  Comment out the appropriate line in
#link:../init.rb if that is a problem, or if you applied the patch yourself.
module DynamicScaffoldResourcePatchActiveRecordHelper
  module OverwriteForm
    #Alias the original method to the new modified method
    def self.included(base)
      base.class_eval do
        alias_method :form, :modified_form
      end
    end
    #Adds options[:url] and options[:method] to the original form method, so
    #it can be called in a manner that works with the Rails RESTful code.
    def modified_form(record_name, options = {})
      record = instance_variable_get("@#{record_name}")

      options = options.symbolize_keys
      options[:action] ||= record.new_record? ? "create" : "update"
      action = options[:url] || url_for(:action => options[:action], :id => record)

      submit_value = options[:submit_value] || options[:action].gsub(/[^\w]/, '').capitalize

      method = options[:method] == :get ? 'get' : 'post'

      contents = ''
      contents << hidden_field_tag('_method', options[:method]) unless options[:method].nil? 
      contents << hidden_field(record_name, :id) unless record.new_record?
      contents << all_input_tags(record, record_name, options)
      yield contents if block_given?
      contents << submit_tag(submit_value)

      content_tag('form', contents, :action => action, :method => method, :enctype => options[:multipart] ? 'multipart/form-data': nil)
    end
  end
end